package com.ntd.frame.haikang;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ntd.frame.haikang.remote.RemoteHaiKang;
import com.ntd.frame.haikang.remote.enums.StreamUrlProtocolEnum;
import ntd.util.UtilLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: yyd
 * Date: 2021-05-18 16:22
 */
public class CameraInfoServiceImplTest {

    private static List<String> names = new ArrayList<>(){{
        add("新大河入海口监测点球机");
        add("新大河入海口监测点枪机");
        add("新大河项目部桥监测点球机");
        add("新大河项目部桥监测点枪机");
        add("新大河碧洲桥监测点球机");
        add("新大河碧洲桥监测点枪机");
    }};

    private static JSONArray jsonArray = new JSONArray();

    public static void main(String[] args) {
        String res = RemoteHaiKang.getStreamUrlByCameraIndexCode("e5105d42c8d141a48bc3ea39630b5683", StreamUrlProtocolEnum.PROTOCOL_HLS.getProtocolName());
        JSONObject jsonObject = JSONUtil.parseObj(res);
        UtilLog.info(jsonObject);
    }

    private static void test1() {
        getIndexCodeByName("root000000");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            UtilLog.info(""
                    + "cameraIndexCode - " + jsonObject.getStr("cameraIndexCode")
                    + ", cameraName - " + jsonObject.getStr("cameraName")
                    + ", regionIndexCode - " + jsonObject.getStr("regionIndexCode")
            );
        }
    }

    public static void getIndexCodeByName(String parentIndexCode) {
        String res = RemoteHaiKang.ListRegionByParentIndexCode(parentIndexCode);
        if (JSONUtil.isJson(res)) {
            JSONObject jsonObject = JSONUtil.parseObj(res);
            JSONArray data = jsonObject.getJSONArray("data");
            if (data.size() < 1) {
                getCameraInfos(parentIndexCode);
                return;
            }
            for (int i = 0; i < data.size(); i++) {
                JSONObject item = data.getJSONObject(i);
                String indexCode = item.getStr("indexCode");
//                UtilLog.info("区域数据 - " + item);
                getIndexCodeByName(indexCode);
            }
        }
    }

    private static void getCameraInfos(String regionIndexCode) {
        JSONObject jsonObject = JSONUtil.parseObj(RemoteHaiKang.listCameraByRegionIndexCode(regionIndexCode));
        JSONArray list = jsonObject.getJSONObject("data").getJSONArray("list");
        if (list == null) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            JSONObject item = list.getJSONObject(i);
            String cameraName = item.getStr("cameraName");
//            UtilLog.info("监控数据 - " + item);
            if (names.contains(cameraName)) {
                jsonArray.add(item);
            }
        }
    }
}
